﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace Jewelnoid
{
    class Interface
    {
        private Vector2 windowSize;
        private List<Pallet> palletsLife;
        private List<Ball> ballsLife;
        private Text2D lifeText;
        private Texture2D palletTexture;
        private Texture2D ballTexture;
        private int oldLifes;

        public Score score;
        public int lifes;

        public Interface(SpriteFont spriteFont, Texture2D palletTexture, Texture2D ballTexture, Vector2 windowSize)
        {
            this.windowSize = windowSize;
            this.ballTexture = ballTexture;
            this.palletTexture = palletTexture;

            this.oldLifes = this.lifes = 1;

            createLifesImages();

            String initialText = "x1";

            this.lifeText = new Text2D(spriteFont, initialText, new Vector2(this.palletsLife[0].position.X + this.palletsLife[0].texture.Width * this.palletsLife[0].scale.X, this.windowSize.Y - spriteFont.MeasureString(initialText).Y), Color.White);
          
            this.score = new Score(spriteFont, new Vector2(0, 0));
        }

        public void update()
        {
            if (this.oldLifes != this.lifes)
            {
                createLifesImages();
                this.lifeText.text = String.Concat("x", this.lifes);
            }

            this.score.update();
            this.score.position = this.windowSize - this.score.spriteFont.MeasureString(this.score.text);
            this.score.position.X -= 5;   //pequeno ajuste, apenas para o score não ficar colado na lateral direita
        }

        public void draw(SpriteBatch spriteBatch)
        {
            this.score.draw(spriteBatch);

            for (int i = 0; i < this.lifes; i++)
            {
                if (this.lifes <= 3)
                {
                    if (this.oldLifes >= this.lifes)
                    {
                        this.palletsLife[i].draw(spriteBatch);
                        this.ballsLife[i].draw(spriteBatch);
                    }
                }
                else
                {
                    this.palletsLife[0].draw(spriteBatch);
                    this.ballsLife[0].draw(spriteBatch);
                    this.lifeText.draw(spriteBatch);
                }
            }
        }

        public void reset(int lifes)
        {
            this.oldLifes = 0;

            this.score.scoreNumber = 0;
            this.score.update();

            this.lifes = lifes;

            update();
        }

        private void createLifesImages()
        {
            float scale = 0.5f;

            palletsLife = new List<Pallet>();
            ballsLife = new List<Ball>();

            for (int i = 0; i < this.lifes; i++)
            {
                Pallet pallet = new Pallet(palletTexture, new Vector2(0, 0), 0);
                Ball ball = new Ball(ballTexture, new Vector2(0, 0), 0);

                pallet.scale = new Vector2(scale, scale);
                ball.scale = new Vector2(scale, scale);
                pallet.position = new Vector2(palletTexture.Width * scale + (i*palletTexture.Width*scale) + 5*i, windowSize.Y - (palletTexture.Height * scale));
                ball.position = new Vector2(pallet.position.X, pallet.position.Y - (ballTexture.Height * scale));

                palletsLife.Add(pallet);
                ballsLife.Add(ball);
            }

            oldLifes = lifes;
        }
    }
}
